﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using System.Collections.Generic;

namespace SkyDriveConnector
{ 
    public partial class GenericViewModel<T> : DependencyObject
    {

        IConnectorOperations connectorOperations;

        ObservableCollection<T> itemsCollection = new ObservableCollection<T>();
        
        

        public string ItemPath { get; set; }

        public string SharePointPath { get; set; }

        public ObservableCollection<T> ItemsCollection
        {
            get
            {
                return itemsCollection;
            }
            set
            {
                itemsCollection = value;
            }

        }

        

        private SharePointModel _localSpInstance { get; set; }

        public GenericViewModel() : this(new SharePointModel()) { }

        public GenericViewModel(string AuthToken) : this(AuthToken , new SkyDriveModel()){}

        private GenericViewModel(string AuthToken, IConnectorOperations ModelInstance)  : this(ModelInstance)
        {
            connectorOperations = ModelInstance;
            connectorOperations.AuthToken = AuthToken;
           
        }
         
   

        private GenericViewModel(IConnectorOperations ModelInstance)
        {
            connectorOperations = ModelInstance;
            _localSpInstance = new SharePointModel();

            connectorOperations.SharePointItemsLoadingComplete += new EventHandler<EventArgs>(connectorOperations_SharePointItemsLoadingComplete);
            connectorOperations.AddSharePointFolderComplete +=new EventHandler<EventArgs>(connectorOperations_AddSharePointFolderComplete);
            connectorOperations.AddSharePointItemComplete +=new EventHandler<EventArgs>(connectorOperations_AddSharePointItemComplete);
            connectorOperations.SharePointItemLoadingOnError += new EventHandler<ItemLoadingErrorEventArgs>(connectorOperations_SharePointItemLoadingOnError);
            connectorOperations.DeleteSharePointItemComplete += new EventHandler<EventArgs>(connectorOperations_DeleteSharePointItemComplete);

            _localSpInstance.SharePointItemsLoadingComplete += new EventHandler<EventArgs>(connectorOperations_SharePointItemsLoadingComplete);
            _localSpInstance.AddSharePointFolderComplete += new EventHandler<EventArgs>(connectorOperations_AddSharePointFolderComplete);
            _localSpInstance.AddSharePointItemComplete += new EventHandler<EventArgs>(connectorOperations_AddSharePointItemComplete);
            _localSpInstance.SharePointItemLoadingOnError += new EventHandler<ItemLoadingErrorEventArgs>(connectorOperations_SharePointItemLoadingOnError);
            _localSpInstance.DeleteSharePointItemComplete += new EventHandler<EventArgs>(connectorOperations_DeleteSharePointItemComplete);


            connectorOperations.AddSkyDriveFolderComplete +=new EventHandler<EventArgs>(connectorOperations_AddSkyDriveFolderComplete);
            connectorOperations.AddSkyDriveItemComplete +=new EventHandler<EventArgs>(connectorOperations_AddSkyDriveItemComplete);            
            connectorOperations.DeleteSkyDriveItemComplete +=new EventHandler<EventArgs>(connectorOperations_DeleteSkyDriveItemComplete);
            connectorOperations.SkyDriveItemsLoadingComplete+=new EventHandler<EventArgs>(connectorOperations_SkyDriveItemsLoadingComplete);            
            connectorOperations.SkyDriveItemLoadingOnError +=new EventHandler<ItemLoadingErrorEventArgs>(connectorOperations_SkyDriveItemLoadingOnError);

        }

        

        

        public void GetItems(string Path)
        {
            int correlationId= App.CorrelationId;

            AddResponseMessage(correlationId, "SkyDrive GetItems Started");
            connectorOperations.GetItems(Path, correlationId);
        }

        public void GetSingleItem(string ItemId)
        {
            int correlationId = App.CorrelationId;
            AddResponseMessage(correlationId, "SharePoint GetSingleItem Started");
            connectorOperations.GetSingleItem(ItemId, correlationId);
        }


        

        public void AddNewItem(string path, SkyDriveObject skydriveObject)
        {
            int correlationId = App.CorrelationId;
            AddResponseMessage(correlationId, string.Format("Adding Item {0} to SharePoint....", skydriveObject.name));
            connectorOperations.AddNewItem(path, skydriveObject, correlationId);
        }

        public void AddNewItem(string path, FileStream filestream, string filename)
        {
            int correlationId = App.CorrelationId;
            AddResponseMessage(correlationId, string.Format("Adding File {0} to SkyDrive....", filename));
            connectorOperations.AddNewItem(path, filestream, filename, correlationId);

        }

        



        public void DeleteItem(string ItemId)
        {
            int correlationId = App.CorrelationId;
            AddResponseMessage(correlationId, "SkyDrive Delete Started");
            connectorOperations.DeleteItem(ItemId, correlationId);
        }


        public void DeleteSharePointItem(string ItemId)
        {
            int correlationId = App.CorrelationId;
            AddResponseMessage(correlationId, "SharePoint Delete Started");
            connectorOperations.DeleteItem(ItemId, correlationId);
        }


        

        public void CreateFolder(string path, string foldername, string description, string folderpath, string skydriveObjectId)
        {
            int correlationId = App.CorrelationId;
            AddResponseMessage(correlationId, string.Format("Creating Folder {0} on SkyDrive....", foldername));
            connectorOperations.CreateFolder(path, foldername, description, correlationId, skydriveObjectId);

            CreateSharePointFolder(folderpath, foldername, skydriveObjectId);

            //int correlationId1 = App.CorrelationId;
            //AddResponseMessage(correlationId1, "SharePoint CreateFolder Started");
            //_localSpInstance.CreateFolder(folderpath, foldername, correlationId1);
        }

        public void CreateSharePointFolder(string folderpath, string foldername,string skydriveObjectId)
        {
            int correlationId = App.CorrelationId;
            AddResponseMessage(correlationId, string.Format("Creating Folder {0} on SharePoint....", foldername));
            _localSpInstance.CreateFolder(folderpath, foldername,"", correlationId, skydriveObjectId);
        }


        
    }
}
